<?php

namespace App\Admin\Controllers\Category;

use App\Admin\Repositories\Category\Category;
use App\Models\Category\Category as CategoryModel;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Http\Controllers\AdminController;

class CategoryController extends AdminController
{
    /**
     * 标题
     */
    protected $title = '分类管理';

    /**
     * 列表页面
     */
    protected function grid()
    {
        return Grid::make(new Category(), function (Grid $grid) {
            $grid->column('id', 'ID')->sortable();
            $grid->column('name', '名称')->tree();
            $grid->column('module', '文件名称')->editable();
            $grid->column('isbn', 'ISBN')->editable();
            $grid->column('image', '封面')->image('', 50, 50);
            $grid->column('page', '页码')->editable()->sortable();
            $grid->column('level', '层级')->badge();
            $grid->column('created_at', '创建时间');
            $grid->column('updated_at', '修改时间')->sortable();

            // 默认排序
            $grid->model()->orderBy('level')->orderBy('page');

            // 禁用操作
            $grid->disableCreateButton();
            $grid->disableActions();
            $grid->disableBatchDelete();

            // 筛选器
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id', 'ID');
                $filter->like('name', '名称');
                $filter->like('module', '文件名称');
                $filter->equal('level', '层级')->select([
                    0 => '顶级',
                    1 => '一级',
                    2 => '二级',
                    3 => '三级',
                ]);
            });

            // 工具栏
            $grid->tools(function (Grid\Tools $tools) {
                $tools->append('<a href="javascript:void(0)" class="btn btn-primary btn-sm" onclick="location.reload()">刷新数据</a>');
            });
        });
    }

    /**
     * 表单页面
     */
    protected function form()
    {
        return Form::make(new Category(), function (Form $form) {
            $form->display('id', 'ID');
            $form->text('name', '名称')->required()->help('分类名称');
            $form->text('isbn', 'ISBN')->help('图书ISBN编号');
            $form->text('module', '文件名称')->help('模块文件名');
            $form->image('image', '封面')->help('分类封面图片');
            $form->number('page', '页码')->default(0)->min(0)->help('用于排序');

            // 父级分类选择
            $form->select('parent_id', '父级分类')
                ->options(function() {
                    return CategoryModel::selectOptions();
                })
                ->default(0)
                ->help('选择父级分类，不选则为顶级分类');

            $form->text('unit_main_name', '主名称')->help('单元主名称');
            $form->text('unit_name', '副标题')->help('单元副标题');

            // 隐藏字段，由模型自动计算
            $form->hidden('level');
            $form->hidden('path');
            $form->hidden('is_directory')->default(0);

            $form->display('created_at', '创建时间');
            $form->display('updated_at', '更新时间');
        });
    }
}
